package fdts.test;

import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Iterator;

import prefuse.action.layout.Layout;
import prefuse.visual.DecoratorItem;
import prefuse.visual.VisualItem;

public class LabelLayout extends Layout{

	public LabelLayout(String group) {
        super(group);
    }
	
	@Override
	public void run(double frac) {
		Iterator iter = m_vis.items(m_group);
		
		ArrayList<DecoratorItem> arr = new ArrayList<DecoratorItem>();
        while ( iter.hasNext() ) {
        	arr.add((DecoratorItem)iter.next());
        }
        
        double hi = arr.get(0).getDecoratedItem().getBounds().getCenterY();
        double lo = arr.get(arr.size()-1).getDecoratedItem().getBounds().getCenterY();
       
        if(!arr.isEmpty()){
	        VisualItem firstDecoratedItem = arr.get(0).getDecoratedItem();
			Rectangle2D firstBounds = firstDecoratedItem.getBounds();
				
			VisualItem secondDecoratedItem = arr.get(24).getDecoratedItem();
			Rectangle2D secondBounds = secondDecoratedItem.getBounds();
			
	        double breadth = (secondBounds.getCenterX() - firstBounds.getCenterX());
			
	        for(int i=0; i<arr.size(); i++){
	        	VisualItem decoratedItem = arr.get(i).getDecoratedItem();
				Rectangle2D bounds = decoratedItem.getBounds();
				
				setX(arr.get(i), null, bounds.getCenterX() + breadth/2 );
				setY(arr.get(i), null, ( (hi - bounds.getCenterY()) + lo));
	        }
        }
	}
}